package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
//指定全局兜底fallback方法
@DefaultProperties(defaultFallback = "payment_Gloval_FallvackMethod")
public class OrderController {
    @Autowired
    private PaymentService paymentService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    @HystrixCommand //不需要配置特定的服务降级方法 适配全局fallback
    public String paymentInfo_OK(@PathVariable("id")Integer id){
        return paymentService.paymentInfo_OK(id);
    }

    @GetMapping("/consumer/payment/hystrix/bad/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_BadHandler",commandProperties = {
            //就是当方法调用时间超过了3秒时就掉用兜底方法paymentInfo_BadHandler 这里可以有多个降级的异常
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymentInfo_Bad(@PathVariable("id")Integer id){
        long time = new Date().getTime();
        return paymentService.paymentInfo_Bad(id) + (new Date().getTime() - time);
    }

    public String paymentInfo_BadHandler(@PathVariable("id")Integer id){
        //特定的fallback方法
        return "当前业务繁忙，请售后再试，id: "+id;
    }

    public String payment_Gloval_FallvackMethod(){
        //全局的fallback 方法
        return "Floval异常处理信息，请稍后再试";
    }


}
